#include "PrecompiledHeader.h"
#include "Color.h"

/*--------------------------------------------------------------------------*/
const Color Color::Red		= Color(1.0f, 0.0f, 0.0f, 1.0f);
const Color Color::Green	= Color(0.0f, 1.0f, 0.0f, 1.0f);
const Color Color::Blue		= Color(0.0f, 0.0f, 1.0f, 1.0f);
const Color Color::Yellow	= Color(1.0f, 1.0f, 0.0f, 1.0f);
const Color Color::Pink		= Color(1.0f, 0.0f, 1.0f, 1.0f);
const Color Color::Cyan		= Color(0.0f, 1.0f, 1.0f, 1.0f);
const Color Color::White	= Color(1.0f, 1.0f, 1.0f, 1.0f);
const Color Color::Black	= Color(0.0f, 0.0f, 0.0f, 1.0f);
/*--------------------------------------------------------------------------*/

Color::Color() :
	r(), g(), b(), a(1.0)
{

}

/*--------------------------------------------------------------------------*/

Color::Color(char c)
{
	this->SetFromChar(c);
}

/*--------------------------------------------------------------------------*/

Color::Color(float r, float g, float b) :
	r(r), g(g), b(b), a(1.0)
{
	this->Repair();
}

/*--------------------------------------------------------------------------*/

Color::Color(float r, float g, float b, float a) :
	r(r), g(g), b(b), a(a)
{
	this->Repair();
}

/*--------------------------------------------------------------------------*/

Color::Color(uchar r, uchar g, uchar b, uchar a) :
	r((float) r / 255.0f), g((float) g / 255.0f), b((float) b / 255.0f), a((float) a / 255.0f)
{
	this->Repair();
}

/*--------------------------------------------------------------------------*/

Color::Color(int r, int g, int b, int a) :
	r((float) r / 255.0f), g((float) g / 255.0f), b((float) b / 255.0f), a((float) a / 255.0f)
{
	this->Repair();
}

/*--------------------------------------------------------------------------*/

Color::Color(uint r, uint g, uint b, uint a) :
	r(r / 255.0f), g(g / 255.0f), b(b / 255.0f), a(a / 255.0f)
{
	this->Repair();
}

/*--------------------------------------------------------------------------*/

Color Color::operator +(const Color& p) const
{
	Color tmp(this->r + p.r, this->g + p.g, this->b + p.b, this->a + p.a);
	tmp.Repair();
	return tmp;
}

/*--------------------------------------------------------------------------*/

Color Color::operator -(const Color& p) const
{
	Color tmp(this->r - p.r, this->g - p.g, this->b - p.b, this->a - p.a);
	tmp.Repair();
	return tmp;
}

/*--------------------------------------------------------------------------*/

Color Color::operator *(float l) const
{
	Color tmp(this->r * l, this->g * l, this->b * l, this->a * l);
	tmp.Repair();
	return tmp;
}

/*--------------------------------------------------------------------------*/

void Color::Repair()
{
	if (this->r > 1.0f)
	{
		this->r = 1.0f;
	}

	if (this->g > 1.0f)
	{
		this->g = 1.0f;
	}

	if (this->b > 1.0f)
	{
		this->b = 1.0f;
	}

	if (this->a > 1.0f)
	{
		this->a = 1.0f;
	}

	if (this->r < 0.0f)
	{
		this->r = 0.0f;
	}

	if (this->g < 0.0f)
	{
		this->g = 0.0f;
	}

	if (this->b < 0.0f)
	{
		this->b = 0.0f;
	}

	if (this->a < 0.0f)
	{
		this->a = 0.0f;
	}
}

/*--------------------------------------------------------------------------*/

void Color::SetFromChar(char color)
{
	switch (color)
	{
	case 'r':
	{
		*this = Color::Red;
		break;
	}

	case 'g':
	{
		*this = Color::Green;
		break;
	}

	case 'b':
	{
		*this = Color::Blue;
		break;
	}

	case 'y':
	{
		*this = Color::Yellow;
		break;
	}

	case 'p':
	{
		*this = Color::Pink;
		break;
	}

	case 'c':
	{
		*this = Color::Cyan;
		break;
	}

	case 'w':
	{
		*this = Color::White;
		break;
	}

	case 'x':
	{
		*this = Color::Black;
		break;
	}

	default:
	{
		*this = Color::White;
	}
	}
}

/*--------------------------------------------------------------------------*/

Color Color::Random()
{
	return Color(rand() % 256, rand() % 256, rand() % 256, 255);
}

/*--------------------------------------------------------------------------*/

std::ostream& operator<<(std::ostream& str, const Color& color)
{
	str << "rgba(" << color.r << ", " << color.g << ", " << color.b << ", " << color.a << ")";

	return str;
}

/*--------------------------------------------------------------------------*/

bool operator!=(const Color& c, const Color& d)
{
	return (c.r != d.r) || (c.g != d.g) || (c.b != d.b) || (c.a != d.a);
}


/*--------------------------------------------------------------------------*/